package shy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import shy.bean.Adoptive;
import shy.bean.Animal;
import shy.mapper.AnimalMapper;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/animal")
public class AnimalController {
    @Resource
    public AnimalMapper mapper;

    @RequestMapping("/findAll")
    public PageInfo<Animal> find(@RequestParam(defaultValue = "1") int page) {
        //引用分页操作
        PageHelper.startPage(page, 10);
        //全查询
        List<Animal> list = mapper.findAllAnimal();
        // 拆箱装箱
        //返回分页信息：当前数据，总页数，上一页，下一页等；
        PageInfo<Animal> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @RequestMapping("/findAnimal")
    public PageInfo<Animal> findAnimal(String animalName, int gender, String health, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        PageHelper.startPage(page, size);
        List<Animal> list = mapper.findAnimalGetBy(gender, "%" + animalName + "%", health, (page - 1) * size, size);
        PageInfo<Animal> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @RequestMapping("/delete")
    public int delete(int id){
        return  mapper.deleteAnimalById(id);
    }
    @RequestMapping("/save")
    public int save(Animal animal){
        return mapper.saveAnimal(animal);
    }
    @RequestMapping("/update")
    public int updateAnimal(Animal animal){
        return mapper.updateAnimal(animal);
    }


}
